<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--引入vue-->
    <script src="js/vue.js"></script>
    <!--引入elementui-->
    <!-- 引入样式 -->
    <link rel="stylesheet" href="js/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="js/element-ui/lib/index.js"></script>
    <!--axios-->
    <script src="js/axios-0.18.0.js"></script>
    <!--jscookie-->
    <script src="js/js.cookie.min.js"></script>
</head>
<style>
    body {
        background: url("imgs/bg1.gif") no-repeat 0px 0px /100%;
    }

    .login {
        width: 450px;
        border: 1px solid rgb(237, 234, 234);
        margin: 60px auto;
        padding-right: 20px;
        border-radius: 10px;
        height: 400px;
    }

    .button {
        padding-left: 30px;
    }

    .elform {
        width: 400px;

    }
</style>
<body>
<div id="app">
    <div class="login">
        <h1 style="text-align: center;color: aliceblue;">高合用户注册</h1>
        <el-form class="elform" status-icon :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPassword">
                <el-input type="password" v-model="ruleForm.checkPassword"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
                <el-input v-model="ruleForm.code">
                    <!--                        <img slot="append" id="code" @click="getCode" src="imgs/a.png" alt="">-->
                    <template slot="append">{{codeGet}}</template>
                </el-input>
            </el-form-item>
        </el-form>
        <div class="button">
            <el-button type="primary" @click="submitForm">注 册</el-button>
            <el-button @click="resetForm('ruleForm')">重 置</el-button>
            <el-button type="success" @click="goLogin">去登录</el-button>
            <el-button @click="getCode">重新获取验证码</el-button>
        </div>
    </div>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPassword !== '') {
                        this.$refs.ruleForm.validateField('checkPassword');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: "",
                    password: '',
                    checkPassword: '',
                    code: ''
                },
                codeGet: '',
                rules: {
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPassword: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                }
            };
        },
        mounted() {
            this.getCode();
        },
        methods: {
            // 获取验证码
            getCode() {
                console.log("logs")
                //获取随机四位数字
                axios.get("/user/code").then(res => {
                    this.codeGet = res.data.data
                })

            },
            // 登录提交表单
            submitForm() {
                console.log(this.ruleForm)
                _this = this
                axios.post("/user/register", this.ruleForm)
                    .then(res => {
                        if (res.data.code == 20060) {
                            this.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            setInterval(function () {
                                _this.goLogin()
                            }, 1000)
                        } else if (res.data.code == 20061) {
                            //验证码错误 重新获取验证码
                            this.$message.error(res.data.msg);
                            _this.ruleForm.code ='';
                            // this.getCode();
                        } else if (res.data.code == 20062) {
                            //用户名存在
                            this.$message.error(res.data.msg);
                            //清空表单
                            this.resetForm()
                            this.getCode()
                        }
                    })

            },
            //重置表单
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            goLogin() {
                location.href = "login.html"
            }
        }
    })
</script>
</html>